<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/24
 * Time: 11:17
 */
namespace App\Forms;

use App\Exceptions\RequestException;
use App\Models\Good;
use App\Models\GoodsFile;

class GoodsFileForms extends BaseForm
{

    /**
     * @param $validated
     * @return bool
     * @throws RequestException
     * @throws \Throwable
     */
    public function updateOrCreateFile($validated)
    {
        $arr = [
            'banner_images'   => GoodsFile::BANNER,
            'detail_images'   => GoodsFile::DETAIL,
//            'param_images'    => GoodsFile::PARAM,
            'download_images' => GoodsFile::D_IMAGE,
        ];

        \DB::beginTransaction();
        try {
            //图片为多张
            foreach ($validated as $k => $v) {
                if (is_array($v) && count($v)) {
                    GoodsFile::where('goods_id', $validated['id'])->where('type', $arr[$k])->delete();
                    foreach ($v as $item) {
                        GoodsFile::create([
                            'goods_id' => $validated['id'],
                            'url'      => $item,
                            'type'     => $arr[$k]
                        ]);
                    }
                }
            }

            //视频为一张，所以分开写
            if (isset($validated['download_video'])) {
                GoodsFile::updateOrCreate(['goods_id' => $validated['id'], 'type' => GoodsFile::D_VIDEO], [
                    'goods_id' => $validated['id'],
                    'url'      => $validated['download_video'],
                    'type'     => GoodsFile::D_VIDEO
                ]);
            }
            \DB::commit();

            return true;
        }catch (\Exception $e){
            \DB::rollBack();

            throw new RequestException($e->getMessage());
        }
    }


    /**
     * 详情
     * @param $goods
     * @return array
     */
    public function getGoodsFile($id)
    {
        $file = GoodsFile::where('goods_id', $id)->get(['type','url']);

        $banner = $detail = $param = $downloadImage = [];

        $downloadVideo = '';

        if(count($file)){

            foreach ($file as $item){
                switch ($item->type){
                    case GoodsFile::BANNER:
                        array_push($banner, $item->url);
                        break;
                    case GoodsFile::DETAIL:
                        array_push($detail, $item->url);
                        break;
//                    case GoodsFile::PARAM:
//                        array_push($param, $item->url);
                        break;
                    case GoodsFile::D_IMAGE:
                        array_push($downloadImage, $item->url);
                        break;
                    case GoodsFile::D_VIDEO:
                        $downloadVideo =  $item->url;
                        break;
                    default:
                        break;
                }
            }
        }

        //增加主图为幻灯片首张
        $banner = array_merge([Good::find($id)->images], $banner);

        return compact( 'banner', 'detail', 'param', 'downloadImage', 'downloadVideo','id');
    }


}